<?php
namespace Admin_\Controller;
use Think\Controller;
class InfoController extends Controller {
    public function __construct()
    {
        parent::__construct();
        $this->_login = A('Login');
        //判断是否登录
        if(!$this->_login->check_login()){
            $this->_login->to_login();die;
        }
    }

    /**
     * 查看记录列表
     */
    public function index(){
        $info_list = M('login_info')->alias('as i')->field('i.*,count(l.id) as num')->where(array(
            'status' => 1,
            'is_delete' => 0,
        ))->join('__LOGIN_LOG__ as l on i.id = l.i_id','left')->group('i.id')->select();
        $go_url = M('config')->where(array('name'=>'go_url'))->find()['value'];
        $this->assign('info_url',$go_url);
        $this->assign('info_list',$info_list);
        $this->display();
    }


    /**
     * 添加一条记录
     */
    public function add(){
        if(IS_POST){
            if(!I('post.name'))
                JSON(array('code'=>201,'message'=>'名称不能为空'));
            if(!$url = I('post.tourl'))
                JSON(array('code'=>203,'message'=>'网址不能为空'));
            if(!preg_match_all('/^(http|https):\/\/[\w-]+\.[\w-]+(\.[\w-]+)*/',$url,$preg_res)){
                JSON(array('code'=>205,'message'=>'请填写正确的网址'));
            }
            $_POST['url'] = $preg_res[0][0];
//            if($this->check_url_exists($_POST['url']))
//                JSON(['code'=>209,'message'=>'网址已存在']);
            $_POST['goid'] = $this->get_infoid();
            $_POST['create_time'] = time();
            $_POST['ip'] = get_client_ip();
            if(D('login_info')->create() && D('login_info')->add()){
                JSON(array('code'=>200,'message'=>'添加成功','data'=>array('go_url'=>U('Info/index'))));
            }
            JSON(array('code'=>207,'message'=>'添加失败'));
        }
        $this->display();
    }

    /**
     * 修改一条记录
     */
    public function edit(){
        $id = I('get.id')+0;
        if($id){
            if(IS_POST){
                if(!I('post.name'))
                    JSON(array('code'=>301,'message'=>'名称不能为空'));
                if(!$url = I('post.tourl'))
                    JSON(array('code'=>303,'message'=>'网址不能为空'));
                if(!preg_match_all('/^(http|https):\/\/[\w-]+\.[\w-]+(\.[\w-]+)*/',$url,$preg_res)){
                    JSON(array('code'=>305,'message'=>'请填写正确的网址'));
                }
                $_POST['url'] = $preg_res[0][0];
                $_POST['id'] = $id;
                if(D('login_info')->create() && D('login_info')->save()){
                    JSON(array('code'=>300,'message'=>'修改成功','data'=>array('go_url'=>U('Info/index'))));
                }
                JSON(array('code'=>307,'message'=>'修改失败'));
            }
            $info = M('login_info')->where(array(
                'status' => 1,
                'is_delete' => 0,
                'id' => $id,
            ))->find();
            if(!$info)
                header(' : '.U('Info/index'));
            $this->assign('info',$info);
            $this->display();
        }
    }

    public function del(){
        $id = I('get.id')+0;
        if($id){
            $del_res = M('login_info')->where(array('id'=>$id))->setField('is_delete',1);
            if($del_res)
                JSON(array('code'=>400,'message'=>'删除成功'));
        }
        JSON(array('code'=>401,'message'=>'删除失败'));
    }

    /**
     * 获得一个唯一的id
     */
    protected function get_infoid(){
        $info_id = $this->make_infoid();
        if(D('login_info')->where(array('goid'=>$info_id))->find())
            return $this->get_infoid();
        return $info_id;
    }

    /**
     * 生成一个唯一的id
     */
    protected function make_infoid(){
        $str = 'weixin';
        $rand_str = 'abcdefghijklnmopqrstuvwxyzABCDEFGHIJKLNMOPQRSTUVWXYZ';
        $rand_str = substr(str_shuffle(str_repeat($rand_str,10)),0,10);
        return $str.$rand_str;
    }

    /**
     * 检查网址是否存在
     */
    public function check_url_exists($url){
        if(D('login_info')->where(array('url'=>$url,'status'=>1,'is_delete'=>0))->find())
            return true;
        return false;
    }
}
